package com.example.understandvideo.service;

import com.example.understandvideo.signer.Credentials;
import com.example.understandvideo.signer.Signer;
import com.example.understandvideo.tool.JsonUtils;
import com.example.understandvideo.tool.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;


import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class OCRProcessor {
    public List<String> sendOcrRequest(String picBase64) {
        String result = "";
        /* create credentials */
        Credentials credentials = new Credentials();
        credentials.setAccessKeyID("AKLTNmM4N2EyNmNhMGM0NGI5MDgxOTRmYTg0ZjMyYjBiZDk");
        credentials.setSecretAccessKey("WTJZeE4yVmtNems0T1RCaU5HUXpORGxpWm1GaE5EVTFZV00wWW1GaE1EWQ==");
        credentials.setRegion("cn-north-1");
        credentials.setService("cv");

        /* create signer */
        Signer signer = new Signer();

        /* create http client */
        CloseableHttpClient httpClient = HttpClients.createDefault();


        try {
            /* prepare request */
            HttpPost request = new HttpPost();
            request.setURI(new URI("https://visual.volcengineapi.com?Action=OCRNormal&Version=2020-08-26"));

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("image_base64", picBase64));
            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            signer.sign(request, credentials);

            /* launch request */
            CloseableHttpResponse response = httpClient.execute(request);

            /* status code */
            System.out.println(response.getStatusLine().getStatusCode());   // 200

            /* get response body */
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
            /* close resources */
            response.close();
            httpClient.close();
        } catch (Exception e) {
            e.getMessage();
        }
        return JsonUtils.extractLineTextsFromStrJson(result);
    }

    public static void main(String[] args) {
        OCRProcessor ocrProcessor = new OCRProcessor();
        String filepath = "C:\\Users\\xjc.ARCVIDEO\\Downloads\\学习\\三国地图.jpg";

        String base64 = Tools.readImageV2(filepath);
        List<String> result = ocrProcessor.sendOcrRequest(base64);
        //String data = JsonUtils.extractLineTextsFromStrJson(result);

        System.out.println(result);
        //System.out.println(data);
    }
}
